local GuildWarScheduleInfoCell = app.mvc.createListViewCell(...)
local NewguildIconRender = require("app.newGuild.views.renders.NewguildIconRender")
local CrossServerGuildWarHelper = require("app.crossServer.views.guildWar.CrossServerGuildWarHelper")
--data =
function GuildWarScheduleInfoCell.create(node)
    local sprite = GuildWarScheduleInfoCell.extend(node or cc.CSLoader:createNode(Res.CSGuildWarScheduleInfoCell))
    if node == nil then
        ccui.Helper:doLayout(sprite)
    end
    sprite:init()
    return sprite
end


function GuildWarScheduleInfoCell:init(data)
    self.super.init(self, data)
    self:setContentSize( self._ui.Panel:getContentSize() )

    self._guildIconRender1 = NewguildIconRender.create(self._ui["Panel/icon_1"])
    self._guildIconRender2 = NewguildIconRender.create(self._ui["Panel/icon_2"])

    self._ui.Button_detail:addClickEventListener(function()
        if self._data then
            local matchInfo = self._data.matchInfo
            if matchInfo then
                app:sendMsg("CrossServerController", "popGuildWarFightReport", matchInfo:getId())
            end
        end
    end)

    self._ui["Panel/Panel_data_1"]:setCascadeOpacityEnabled(true)
    self._ui["Panel/Panel_data_2"]:setCascadeOpacityEnabled(true)

    self:refresh()

end

function GuildWarScheduleInfoCell:refresh()
    local data = self._data
    if not data then
        return
    end
    local matchInfo = data.matchInfo
    local wDay = app:getWeekDayPassDailyUpdate()
    if matchInfo then
        local guild1, guild2 = matchInfo:getGuildWithSort()
        if guild1 then
            self._guildIconRender1:setData(guild1:getGuildIconData())
            self._ui["Panel/Panel_data_1/server"]:setString(guild1:getServerName())
            self._ui["Panel/Panel_data_1/guild"]:setString(guild1:getGuildName())
            if wDay >= 6 then
                self._ui["Panel/Panel_data_1/ranking"]:setString(string.format(L("WGWPreGameRank"), guild1:getRank()))
            else
                self._ui["Panel/Panel_data_1/ranking"]:setString(string.format(L("WGWCurrentRank"), guild1:getRank()))
            end
        end

        if guild2 then
            self._ui["Panel/Panel_data_2"]:setVisible(true)
            self._guildIconRender2:setVisible(true)
            
            self._guildIconRender2:setData(guild2:getGuildIconData())
            self._ui["Panel/Panel_data_2/server"]:setString(guild2:getServerName())
            self._ui["Panel/Panel_data_2/guild"]:setString(guild2:getGuildName())
            if wDay >= 6 then
                self._ui["Panel/Panel_data_2/ranking"]:setString(string.format(L("WGWPreGameRank"), guild2:getRank()))
            else
                self._ui["Panel/Panel_data_2/ranking"]:setString(string.format(L("WGWCurrentRank"), guild2:getRank()))
            end
        else
            self._ui["Panel/Panel_data_2"]:setVisible(false)
            self._guildIconRender2:setVisible(false)
        end

        local winner = matchInfo:getWinner()

        self._ui["Panel/Panel_data_1"]:setOpacity(255)
        self._ui["Panel/Panel_data_2"]:setOpacity(255)
        if winner ~= nil then
            self._ui["Panel/ongoing"]:setVisible(false)
            self._ui["Panel/img_state"]:setVisible(true)
            if guild1 then
                if guild1:getGuildId() == matchInfo:getWinner() then
                    self._ui["Panel/bg_state"]:loadTexture(Res.CSGuildWarGuildScheduleBtnBg2, 1)
                    self._ui["Panel/img_state"]:loadTexture(Res.CSGuildWarBattleResultWin)
                    self._ui["Panel/Panel_data_2"]:setOpacity(255 * 0.6)
                else
                    self._ui["Panel/bg_state"]:loadTexture(Res.CSGuildWarGuildScheduleBtnBg3, 1)
                    self._ui["Panel/img_state"]:loadTexture(Res.CSGuildWarBattleResultLose)
                    self._ui["Panel/Panel_data_1"]:setOpacity(255 * 0.6)
                end
            end
        else
            self._ui["Panel/bg_state"]:loadTexture(Res.CSGuildWarGuildScheduleBtnBg1, 1)
            if CrossServerGuildWarHelper.isTodayFightBegin() then
                self._ui["Panel/ongoing"]:loadTexture(Res.CSGuildWarBattleResultFighting)
            else
                self._ui["Panel/ongoing"]:loadTexture(Res.CSGuildWarBattleResultNotStart)
            end

            self._ui["Panel/ongoing"]:setVisible(true)
            self._ui["Panel/img_state"]:setVisible(false)
        end
    end
end


return GuildWarScheduleInfoCell